package com.stepforward.me;


import java.util.ArrayList;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class QuickBus extends Activity {
    /** Called when the activity is first created. */
	BusTimeHelper dbHelper;
	Spinner busnospinner,busstopspinner,businoutboundspinner;
	ListView buslistview; 
	Button quicksearchbutton;
	ArrayList<String> busstop = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quickbus);
        dbHelper = new BusTimeHelper(this);
        
        dbHelper.getAllRoute();
    	busstop.add("64"); busstop.add("61D");busstop.add("59");busstop.add("28X");
        ArrayList<String> busno = dbHelper.getAllRoute();
        
        String[] businoutbound = new String[] {"Inbound", "Outbound"};
        busnospinner = (Spinner) findViewById(R.id.busnospinner);
        buslistview = (ListView) findViewById(R.id.buslistview);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, busstop);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		Button btn = (Button)findViewById(R.id.busnoaddBtn);
		btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EditText newBusNo = (EditText)findViewById(R.id.busnoadd);
				busstop.add(newBusNo.getText().toString());
				newBusNo.setText("");
			       ArrayAdapter<String> adapter = new ArrayAdapter<String>(QuickBus.this,
							android.R.layout.simple_spinner_item, busstop);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					busnospinner.setAdapter(adapter);	
			}
			
		});
        busnospinner.setAdapter(adapter);
        
      
		busstopspinner = (Spinner) findViewById(R.id.busstopspinner);

	
		businoutboundspinner = (Spinner) findViewById(R.id.businoutbountspinner);		
		 adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, businoutbound);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		businoutboundspinner.setAdapter(adapter);
		busnospinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View arg1,
					int position, long id) {
				// TODO Auto-generated method stub
				ArrayList<String> busstop = dbHelper.getStopByRoute((String)parent.getItemAtPosition(position));
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(QuickBus.this,
						android.R.layout.simple_spinner_item, busstop);
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				busstopspinner.setAdapter(adapter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}

		});
		
		busstopspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View arg1,
					int position, long id) {
				// TODO Auto-generated method stub
				
				ArrayList<String> bustime =dbHelper.getTimeByRouteAndStop(busnospinner.getSelectedItem().toString(),(String)parent.getItemAtPosition(position));
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(QuickBus.this,
						android.R.layout.simple_spinner_item, bustime);
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				buslistview.setAdapter(adapter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}

		});
		
		quicksearchbutton = (Button)findViewById(R.id.busquicktimesearch);
		quicksearchbutton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				// TODO Auto-generated method stub
				ArrayList<String> bustime = dbHelper.getTimeByRouteAndStop(busnospinner.getSelectedItem().toString(),busstopspinner.getSelectedItem().toString());
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(QuickBus.this,
						R.layout.list_item, bustime);
		
				buslistview.setAdapter(adapter);
				
			}
			
		});
		
		dbHelper.close();
    }
    
    protected void onDestroy() {
        super.onDestroy();
        dbHelper.close();
    }

   
}